package com.yutel.amote;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.yutel.amote.service.AmoteConstant;
import com.yutel.amote.service.MsgReceiver;
import com.yutel.amote.service.ServerService;

public class ServerCfgActivity extends Activity implements OnClickListener {
	final String TAG = "Server";
	private static String MSG_ACTION = "com.webabcd.service.msg";
	private AmoteConstant con;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.servercfg);
		con = AmoteConstant.getInstance(null);
		Button ok = (Button) findViewById(R.id.serverOK);
		ok.setOnClickListener(this);
		Button start = (Button) findViewById(R.id.serverStart);
		start.setOnClickListener(this);
		Button stop = (Button) findViewById(R.id.serverStop);
		stop.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.serverOK:
			EditText et = (EditText) findViewById(R.id.serverPort);
			Log.d(TAG, "set server port:" + et.getText().toString());
			showMsg("server port save!");
			con.save("serverPort", et.getText().toString());
			break;
		case R.id.serverStart:
			MsgReceiver msg = new MsgReceiver();
			IntentFilter intent = new IntentFilter();
			intent.addAction(MSG_ACTION);
			this.registerReceiver(msg, intent);
			startService(new Intent(ServerCfgActivity.this, ServerService.class));
			showMsg("server status is run");
			break;
		case R.id.serverStop:
			stopService(new Intent(ServerCfgActivity.this, ServerService.class));
			showMsg("server status is stop");
			break;

		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.control, menu);
		menu.removeItem(R.id.menui_client_touch);
		menu.removeItem(R.id.menui_touch_config);
		menu.removeItem(R.id.menui_client_sensor);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menui_appType:
			Intent intent = new Intent(ServerCfgActivity.this,
					ApptypeActivity.class);
			startActivity(intent);
			break;
		case R.id.menui_server_control:
			Log.d(TAG, "menui_server_control");
			break;
		case R.id.menui_client_touch:
			Log.d(TAG, "menui_client_touch");
			break;
		case R.id.menui_touch_config:
			Log.d(TAG, "menui_touch_config");
			break;
		default:
			Log.d(TAG, "default=" + item.getItemId());
		}
		return true;
	}

	public void showMsg(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}
}